<?php

namespace app\common\validate\setting;
use app\common\model\setting\MemberHelpModel;
use app\common\model\setting\SettingPriceAttributesModel;
use app\common\model\setting\SettingPriceRangeModel;
use think\Validate;
/**
 * 价格区间验证器
 */
class SettingPriceRangeValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'platform_ids' => ['require', 'array'],
        'product_type_ids' => ['require', 'array'],
        'merchant_fees' => 'require',
        'user_consumption' => 'require',
        'price_start' => ['require', 'checkPriceStartExisted'],
        'price_close' => 'require',
        'mer_describe' => 'require',
        'increment_type' =>['require', 'checkIncrementTypeExisted'],
        'comment_type' => ['require', 'in' => '0,1,2,3,4', 'checkCommentTypeExisted'],
        'setting_price_range_id' => 'require',
        'values' => ['require', 'array'],
        'code' =>['require', 'checkCodeExisted'],
        'user_help_code' =>['checkHelpCodeExisted'],
    ];
    // 错误信息
    protected $message = [
        'platform_ids.require' => '请选择店铺平台',
        'product_type_ids.require' => '请选择活动分类',
        'merchant_fees.require' => '请输入商家收费',
        'user_consumption.require' => '请输入用户消耗',
        'price_start.require' => '请输入价格起',
        'price_close.require' => '请输入价格止',
        'mer_describe.require' => '请输入商家端描述',
        'increment_type.require' => '请输入增值类型',
        'comment_type.require' => '请输入评价类型',
        'status.require' => '请输入价格类型',
        'setting_price_range_id.require' => '请选择增值佣金',
        'values.require' => '请设置条件',
    ];
    // 验证场景
    protected $scene = [
        'add0'     =>  ['code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','price_start','price_close' ],
        'add1'     =>  ['code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','mer_describe','user_help_code' ],
        'add2'     =>  ['code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','comment_type' ],
        'add3'     =>  ['code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','comment_type'],

        'edit0'     =>  ['id','code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','price_start','price_close' ],
        'edit1'     =>  ['id','code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','mer_describe','user_help_code' ],
        'edit2'     =>  ['id','code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','comment_type' ],
        'edit3'     =>  ['id','code','status','product_type_ids', 'platform_ids','merchant_fees','user_consumption','comment_type'],

        'info'    =>  ['id'],
        'dele'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
        'merchant_fees' =>  ['ids', 'merchant_fees'],
        'user_consumption' =>  ['ids', 'user_consumption'],
        'saveCondition'    =>  ['setting_price_range_id'],
    ];

    /**
     * 增值类型验证
     */
    protected function checkIncrementTypeExisted($value, $rule, $data = [])
    {
//        $setting_price_range_ids = SettingPriceAttributesModel::
//        where('setting_store_platform_id', 'in', $data['platform_ids'])
//        ->whereOr('shop_product_type_id', 'in', $data['product_type_ids'])
//        ->group('setting_price_range_id')
//        ->field('setting_price_range_id')
//        ->select()
//        ->toArray();
//        $setting_price_range_ids = array_column($setting_price_range_ids,'setting_price_range_id');
        $setting_price_range_ids = $this->getRangeIds($data['platform_ids'],$data['product_type_ids']);
        $model = new SettingPriceRangeModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['increment_type', '=', trim($data['increment_type'])];
        $where[] = ['status', '=', $data['status']];
        if(count($setting_price_range_ids)>0){
            $where[] = [$pk, 'in', $setting_price_range_ids];
        }else{
            $where[] = [$pk, 'in', [0]];
        }

        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '增值类型已存在：' . trim($data['increment_type']);
        }

        return true;
    }

    /**
     * 评论类型验证
     */
    protected function checkCommentTypeExisted($value, $rule, $data = [])
    {
//        $setting_price_range_ids = SettingPriceAttributesModel::
//        where('setting_store_platform_id', 'in', $data['platform_ids'])
//            ->whereOr('shop_product_type_id', 'in', $data['product_type_ids'])
//            ->group('setting_price_range_id')
//            ->field('setting_price_range_id')
//            ->select()
//            ->toArray();
//        $setting_price_range_ids = array_column($setting_price_range_ids,'setting_price_range_id');
        $setting_price_range_ids = $this->getRangeIds($data['platform_ids'],$data['product_type_ids']);

        $model = new SettingPriceRangeModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['comment_type', '=', trim($data['comment_type'])];
        $where[] = ['status', '=', $data['status']];
        if(count($setting_price_range_ids)>0){
            $where[] = [$pk, 'in', $setting_price_range_ids];
        }else{
            $where[] = [$pk, 'in', [0]];
        }
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '评论类型已存在：' . trim($data['comment_type']);
        }

        return true;
    }

    //验证起止价格
    protected function checkPriceStartExisted($value, $rule, $data = [])
    {
//        $setting_price_range_ids = SettingPriceAttributesModel::
//        where('setting_store_platform_id', 'in', $data['platform_ids'])
//            ->whereOr('shop_product_type_id', 'in', $data['product_type_ids'])
//            ->group('setting_price_range_id')
//            ->field('setting_price_range_id')
//            ->select()
//            ->toArray();
//        $setting_price_range_ids = array_column($setting_price_range_ids,'setting_price_range_id');
        $setting_price_range_ids = $this->getRangeIds($data['platform_ids'],$data['product_type_ids']);

        $model = new SettingPriceRangeModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;
        $where[] = [$pk, '<>', $id];
        $where[] = ['status', '=', $data['status']];
        if(count($setting_price_range_ids)>0){
            $where[] = [$pk, 'in', $setting_price_range_ids];
            $where[] = ['price_start', '=', $data['price_start']];
            $where[] = ['price_close', '=', $data['price_close']];
        }else{
            $where[] = [$pk, 'in', [0]];
        }
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '价格区间已存在：' . $data['price_start'].'至'.$data['price_close'];
        }
        return true;
    }

    /**
     * 编码验证
     */
    protected function checkCodeExisted($value, $rule, $data = [])
    {
//        $setting_price_range_ids = SettingPriceAttributesModel::
//        where('setting_store_platform_id', 'in', $data['platform_ids'])
//            ->whereOr('shop_product_type_id', 'in', $data['product_type_ids'])
//            ->group('setting_price_range_id')
//            ->field('setting_price_range_id')
//            ->select()
//            ->toArray();
//        $setting_price_range_ids = array_column($setting_price_range_ids,'setting_price_range_id');
        $setting_price_range_ids = $this->getRangeIds($data['platform_ids'],$data['product_type_ids']);

        $model = new SettingPriceRangeModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['code', '=', trim($data['code'])];
        $where[] = ['status', '=', $data['status']];
        if(count($setting_price_range_ids)>0){
            $where[] = [$pk, 'in', $setting_price_range_ids];
        }else{
            $where[] = [$pk, 'in', [0]];
        }
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '编码已存在：' . trim($data['code']);
        }

        return true;
    }
    /**
     * 帮助中心编码验证
     */
    protected function checkHelpCodeExisted($value, $rule, $data = [])
    {
        if(trim($data['user_help_code'])){
            $model = new MemberHelpModel();
            $pk = $model->getPk();
            $where[] = ['code', '=', trim($data['user_help_code'])];
            $where = where_delete($where);
            $info = $model->field($pk)->where($where)->find();
            if (!$info) {
                return '帮助编码不存在：' . trim($data['user_help_code']);
            }
        }
        return true;
    }

    //获取关联表ID
    protected function getRangeIds($platform_ids=[],$product_type_ids=[])
    {
        // 获取符合 platform_ids 条件的 setting_price_range_id
        $platform_range_ids = SettingPriceAttributesModel::alias('a')
            ->leftJoin('setting_price_range','setting_price_range.id = a.setting_price_range_id')
            ->whereIn('setting_price_range.is_delete', 0)
            ->whereIn('a.setting_store_platform_id', $platform_ids)
            ->group('a.setting_price_range_id')
            ->field('a.setting_price_range_id')
            ->select()
            ->toArray();
        $platform_range_ids = array_column($platform_range_ids, 'setting_price_range_id');

        // 获取符合 product_type_ids 条件的 setting_price_range_id
        $product_type_range_ids = SettingPriceAttributesModel::alias('a')
            ->leftJoin('setting_price_range','setting_price_range.id = a.setting_price_range_id')
            ->whereIn('setting_price_range.is_delete', 0)
            ->whereIn('a.shop_product_type_id', $product_type_ids)
            ->group('a.setting_price_range_id')
            ->field('a.setting_price_range_id')
            ->select()
            ->toArray();
        $product_type_range_ids = array_column($product_type_range_ids, 'setting_price_range_id');

        // 取交集
        $setting_price_range_ids = array_intersect($platform_range_ids, $product_type_range_ids);
        return $setting_price_range_ids;
    }

}
